/**
 * Copyright (c) 2006-2008, Julien PORTALIER
 * 
 * Licensed under The LGPL License
 * Redistributions of files must retain the above copyright notice.
 * 
 * @copyright  Copyright (c) 2006-2008, Julien PORTALIER
 * @link       http://feather-cm.google.com/
 * @license    http://www.opensource.org/licenses/lgpl-license.php The LGPL License
 */

_gettext_add_translations("fr",
{
	'File Manager':               'Gestionnaire de fichiers',
	'Emphasis (Ctrl+I)':          'Emphase (Ctrl+I)',
	'Bold (Ctrl+B)':              'Gras (Ctrl+B)',
	'Underline (Ctrl+U)':         'Souligner (Ctrl+U)',
	'Deleted (Ctrl+D)':           'Barré (Ctrl+D)',
	'Create link (Ctrl+L)':       'Créer un lien (Ctrl+L)',
	'Link':                       'Lien',
	'Files':                      'Fichiers',
	'Add an image, video or music (Ctrl+M)': 'Ajouter une image, vidéo ou de la musique (Ctrl+M)',
	'Enter the URL to point to:': "Entrez l'URL vers laquelle le lien doit pointer :",
	'Enter link text:':           "Entrez le titre du lien :",
	'Enter image, video or music location URL:':  "Entrez l'URL de l'image, vidéo ou de la musique :",
	"Tip: to add an image or video click the 'Files' button.": 'Astuce : pour ajouter une image ou vidéo, cliquez le bouton « Fichiers ». ',
	"New folder's name:":         "Nom du nouveau dossier :"
});

Toolbar.Filer = Toolbar.Text.extend(
{
	initialize: function(textarea, filer_url, blankslate)
	{
		this.buttons = [
			['bold',      null, 'b'],
			['italic',    null, 'i'],
			['underline', null, 'u'],
			['del',       null, 'd'],
			[],
			['link',      null, 'l'],
			[],
			['files',     null, 'm'],
			['help',      null, 'h']
		];
		this.filer_url = filer_url || '/filer';
		this.blankslate = blankslate || false;
		this.parent(textarea);
	},
	
	create: function()
	{
		this.toolbar = new Element('span', {'class': this.options.className});
		
		this.addButton('bold',      _('Strong (Ctrl+B)'),    'A', {'font-weight':     'bold'});
		this.addButton('italic',    _('Emphasis (Ctrl+I)'),  'A', {'font-style':      'italic'});
		this.addButton('underline', _('Underline (Ctrl+U)'), 'A', {'text-decoration': 'underline'});
		this.addButton('del',       _('Deleted (Ctrl+D)'),   'A', {'text-decoration': 'line-through'});
		this.addButton('link',      _('Create link (Ctrl+L)'),           _('Link'));
		this.addSeparator();
		this.addButton('files',     _('Add a multimedia file (Ctrl+M)'), _('Files'));
		
		if (this.blankslate) 
		{
			this.addSeparator();
			this.toolbar.appendChild(
				new Element('span', {'class': 'blankslate'})
					.setText(_("Tip: to add an image or video click the 'Files' button."))
			);
		}
		
		this.toolbar.injectBefore(this.textarea);
		
		/*
		this.actions = new Element('span', {'class': 'actions'});
		this.actions.appendChild(new Element('a', {
			'href':  location.protocol + '//' + location.hostname.replace(/^([^.]+[.])/, '') + '/help/webcomics/pages',
			'class': 'help',
			'title': _('Do you need help?')
		}).setText(_('Need help?')));
		this.actions.addEvent('click', this.action.bindWithEvent(this));
		this.actions.injectBefore(this.textarea);
		*/
	},
	
	// callbacks
	
	cb_italic:    function() { this.encloseSelection('//'); },
	cb_bold:      function() { this.encloseSelection('**'); },
	cb_underline: function() { this.encloseSelection('__'); },
	cb_del:       function() { this.encloseSelection('<del>', '</del>'); },
	
	/**
	 * @todo Make something fancier.
	 * @todo Add possibility to enter a title attribute.
	 */
	cb_link: function()
	{
		// URL
		var url = prompt(_("Enter the URL to point to:"));
		if (url)
		{
			url = url.trim();
			
			// TEXT
			var text = this.getSelection();
			if (!text)
				text = prompt(_('Enter link text:'));
			
			this.replaceSelection('"' + text + '":' + url);
		}
	},
	
	cb_files: function()
	{
		if (!this.filer) 
			this.filer = new Filer(this.filer_url, this);
		this.filer.show();
	},
	
	cb_help: function()
	{
		var url = $E('span.actions a.help').getAttribute('href') + '?layout=popup';
		window.open(url, 'help', 'width=500,height=400');
	}
});

/**
 * @param {String} url
 * @param {Object Toolbar.Text} toolbar
 */
var Filer = new Class(
{
	initialize: function(url, toolbar)
	{
		this.url     = url;
		this.toolbar = toolbar;
		this.window  = new Windows.Sticky(this.url, {
			'id':       'filerWindow',
			'title':    _('File Manager'),
			'callback': this.__callback.bind(this)
		});
	},
	
	__callback: function(content)
	{
		// admin tasks
		$ES('.admin .mkdir', content).addEvent('click',  this.__mkdir.bindWithEvent(this));
		$ES('form.upload',   content).addEvent('submit', this.__upload.bindWithEvent(this));
		
		// add content to textarea
		this.addToTextarea = $('addToPage');
		if (this.addToTextarea)
		{
			this.addToTextarea.addEvent('submit',    this.__addToTextarea.bindWithEvent(this));
			$E('p.url a', content).addEvent('click', this.__showImage.bindWithEvent(this));
		}
	},
	
	__mkdir: function(evt)
	{
		evt.stop();
		var folderName = prompt(_("New folder's name:"));
		if (folderName)
		{
			var url = evt.target.getAttribute('href');
			this.window.request('post', url, {'data[new_folder]': folderName});
		}
	},
	
	__upload: function(evt)
	{
		// create hidden iframe
		this.uploadIFrame = new Element('iframe', {
			'name': 'filerUploadIFrame',
			'styles': {
				'display':    'none',
				'visibility': 'hidden'
			}
		}).injectInside(document.body);
		
		// target post to hidden iframe
		evt.target.setAttribute('target', 'filerUploadIFrame');
		
		// event (when file has been uploaded)
		this.uploadIFrame.addEvent('load', this.__afterUpload.bind(this));
	},
	
	__afterUpload: function()
	{
		var xbody = this.uploadIFrame.contentDocument ? this.uploadIFrame.contentDocument.body :
			document.frames['filerUploadIFrame'].document.body;
		
		// returned content
		var content = xbody;
		
		// destroy iframe & content
		this.uploadIFrame.remove.delay(10, this.uploadIFrame);
		
		// set content in window
		this.window.replaceContentWith(content.innerHTML);
	},
	
	__addToTextarea: function(evt)
	{
		evt.stop();
		var src     = $('addToPage_path').value;
		var options = '';
		var before  = '';
		var after   = '';
		
		if (this.addToTextarea.hasClass('image')) 
		{
			var resize  = $('addToPage_resize').value;
			var alt     = $('addToPage_alt').value;
			var title   = $('addToPage_title').value;
			var align   = $('addToPage_align').value;
			
			if (resize != '') options += '|' + resize;
			if (alt)          options += '|' + alt;
			if (title)        options += '|' + title;
			if (align)
			{
				switch(align)
				{
					case 'left':   before = '\n<div class="left">\n\t';   after = '\n</div>'; break;
					case 'right':  before = '\n<div class="right">\n\t';  after = '\n</div>'; break;
					case 'center': before = '\n<div class="center">\n\t'; after = '\n</div>'; break;
					case 'floatleft':  options += '|left';  break;
					case 'floatright': options += '|right'; break;
				}
			}
			
			var str = before + '{{' + src + options + '}}' + after + '\n';
		}
		else if (this.addToTextarea.hasClass('audio'))
		{
			var autoplay = $('addToPage_autoplay').checked;
			if (autoplay)
				options = '|' + 'autoplay';
			
			var str = "{{" + src + options + "}}\n";
		}
		else if (this.addToTextarea.hasClass('video'))
		{
			var size = $('addToPage_resize').value;
			options = '|' + ((size != '') ? size : '480x360');
			
			var str = "{{" + src + options + "}}\n";
		}
		else if (this.addToTextarea.hasClass('flash'))
		{
			var size = $('addToPage_resize').value;
			if (size != '')
				size = size.split('x');
			
			var str = '\n<object type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" data="' + src + '" width="'+ size[0] + '" height="'+ size[1] + '">\n' +
				'  <param name="movie" value="' + src + '" />\n' +
				'  <param name="wmode" value="transparent" />\n' +
				'  <param name="flashVars" value="" />\n' +
			'</object>\n';
		}
		else
		{
			var str = "{{" + src + "}}\n";
		}
		
		// finish
		this.toolbar.insertText(str);
		this.window.hide();
	},
	
	__showImage: function(evt)
	{
		window.open(evt.target.getAttribute('href'), 'filer_show_image',
			'menubar=no,status=no,scrollbars=yes,toolbar=no,width=500,height=400');
		evt.stop();
	},
	
	show: function()
	{
		this.window.show();
	}
});
